package com.chaoxuan.uebt.fragment;

import android.content.Intent;
import android.icu.text.IDNA;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.activity.NewDetailActivity;
import com.chaoxuan.uebt.activity.NoticeActivity;
import com.chaoxuan.uebt.activity.StoreActivity;
import com.chaoxuan.uebt.activity.WebActivity;
import com.chaoxuan.uebt.adapter.AdAdapter;
import com.chaoxuan.uebt.adapter.InfoAdapter;
import com.chaoxuan.uebt.model.Ad;
import com.chaoxuan.uebt.model.News;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import java.util.List;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2017/3/13.
 */
public class FindFragment extends Fragment implements View.OnClickListener,BaseQuickAdapter.OnItemClickListener {
    AdAdapter adapter;
    InfoAdapter infoAdapter;
    String type="2";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        view.findViewById(R.id.btn_notice).setOnClickListener(this);
        view.findViewById(R.id.btn_store).setOnClickListener(this);
        view.findViewById(R.id.img_store).setOnClickListener(this);

        RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        adapter= new AdAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);


        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerView newView = (RecyclerView)view.findViewById(R.id.newView);
        newView.setLayoutManager(linearLayoutManager);
        infoAdapter= new InfoAdapter();
        newView.setAdapter(infoAdapter);
        infoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                News news=(News) adapter.getData().get(position);
                if(news==null)return;
                Intent i=new Intent(getActivity(),NewDetailActivity.class);
                i.putExtra("id",news.getId());
                startActivity(i);
            }
        });

        load();
        return view;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Ad ad=(Ad) adapter.getData().get(position);
        jump(ad.getUrl());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_store:
                startActivity(new Intent(getActivity(),StoreActivity.class));
                break;
            case R.id.btn_store:
                startActivity(new Intent(getActivity(),StoreActivity.class));
                break;
            case R.id.btn_notice:
                startActivity(new Intent(getActivity(),NoticeActivity.class));
                break;
        }
    }

    private void load(){
        RetrofitUtils.getAPIService().indexNotice(0,type).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseSubscriber<List<News>>() {
            @Override
            public void onSuccess(List<News> data) {
                if(data==null)return;
                for(News n:data) {
                    if(infoAdapter.getData().size()==2)break;
                    infoAdapter.addData(n);
                }
            }
        });
        RetrofitUtils.getAPIService().ad().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseSubscriber<List<Ad>>() {
            @Override
            public void onSuccess(List<Ad> ads) {
                adapter.addData(ads);
            }
        });
    }

    private void jump(String url){
        Intent intent = new Intent(getActivity(), WebActivity.class);
        intent.putExtra("url",url);
        getActivity().startActivity(intent);
    }


}
